package com.fionera.sample.fragment

import android.os.Bundle
import android.support.annotation.IntDef
import android.support.v4.content.ContextCompat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout

import com.fionera.sample.R
import com.fionera.sample.widget.TitleBar

abstract class TitleBarFragment : BaseFragment() {
    private lateinit var mTitleBar: TitleBar

    @IntDef(View.VISIBLE, View.INVISIBLE, View.GONE)
    @Retention(AnnotationRetention.SOURCE)
    internal annotation class Visibility

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val mLayoutMain = inflater.inflate(R.layout.title_bar_content, container, false)
        val mLayoutContent = mLayoutMain
                .findViewById<LinearLayout>(R.id.title_bar_content)
        mTitleBar = mLayoutMain.findViewById(R.id.title_bar)
        val mContent = inflater.inflate(setLayoutResource(), null)
        mLayoutContent.addView(mContent, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT)
        return mLayoutMain
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        findViewInThisFunction(view)
    }

    abstract fun setLayoutResource(): Int

    abstract fun findViewInThisFunction(rootView: View)

    fun setTitleBarVisibility(@Visibility visibility: Int) {
        mTitleBar.visibility = visibility
    }

    fun setTitleBarText(title: String) {
        mTitleBar.setTitleBarText(title)
    }

    fun setTitleBarColor(id: Int) {
        mTitleBar.setTitleBarColor(ContextCompat.getColor(mContext, id))
    }

    fun setTitleBarLeft(drawable: Int) {
        mTitleBar.setLeftDrawable(drawable)
    }

    fun setTitleBarRight(drawable: Int) {
        mTitleBar.setRightDrawable(drawable)
    }

    fun setTitleBarLeft(leftText: String) {
        mTitleBar.setLeftText(leftText)
    }

    fun setTitleBarRight(rightText: String) {
        mTitleBar.setRightText(rightText)
    }

    fun setTitleBarLeftVisibility(@Visibility visibility: Int) {
        mTitleBar.setLeftVisibility(visibility)
    }

    fun setTitleBarRight(rightText: String, iconId: Int, iconAtLeft: Boolean) {
        mTitleBar.setRightTextAndIcon(rightText, iconId, iconAtLeft)
    }

    fun setTitleBarLeft(leftText: String, iconId: Int, iconAtLeft: Boolean) {
        mTitleBar.setLeftTextAndIcon(leftText, iconId, iconAtLeft)
    }

    fun setTitleBarRightVisibility(@Visibility visibility: Int) {
        mTitleBar.setRightVisibility(visibility)
    }

    fun setTitleBarLeftClick(onLeftClickListener: View.OnClickListener) {
        mTitleBar.setTitleBarLeftClick(onLeftClickListener)
    }

    fun setTitleBarRightClick(onRightClickListener: View.OnClickListener) {
        mTitleBar.setTitleBarRightClick(onRightClickListener)
    }

    fun setTitleBarRightFirstClick(onRightClickListener: View.OnClickListener) {
        mTitleBar.setTitleBarRightFirstClick(onRightClickListener)
    }

    fun setTitleBarRightFirstVisibility(@Visibility visibility: Int) {
        mTitleBar.setRightFirstVisibility(visibility)
    }

    fun setTitleBarRightFirst(drawable: Int) {
        mTitleBar.setRightFirstDrawable(drawable)
    }

    fun setTitleBarTitleClick(onTitleClickListener: View.OnClickListener) {
        mTitleBar.setTitleBarTitleClick(onTitleClickListener)
    }
}
